package com.gykfbxxh.imovie.utils;

import android.app.Activity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.gykfbxxh.imovie.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuxinghai
 * On 2017/8/31
 */

public class ViewFlipperUtils {
    private static ViewFlipperUtils mInstance;
    public ViewFlipper viewflipper ;
    public ViewFlipperUtils() {
    }

    public static synchronized ViewFlipperUtils getInstance(){
        if(mInstance ==null) {
            mInstance = new ViewFlipperUtils();
        }
        return mInstance;
    }

    List<ViewFlipper> list = new ArrayList<>();

    public void setFlipperView(ViewFlipper viewflipper,List<String> data,Activity activity,int textbg){
        this.viewflipper = viewflipper;
        list.add(viewflipper);
        for (int i = 0; i < data.size(); i++) {
            LinearLayout view = null;
            if(textbg==0) {
                view = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.layout_viewflipper_text, null);
            }else if(textbg==1) {
                view = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.layout_viewflipper_text_bg_yellow, null);
            }else if(textbg == 2) {
                view = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.layout_viewflipper_two_bg, null);
                TextView textView = (TextView) view.findViewById(R.id.textView);
                TextView textView2 = (TextView) view.findViewById(R.id.textView2);
                textView.setText(data.get(i));
                if(i>=data.size()-1) {
                    textView2.setVisibility(View.INVISIBLE);
                }else {
                    textView2.setVisibility(View.VISIBLE);
                    textView2.setText(data.get(i+1));
                    if(TextUtils.isEmpty(data.get(i+1))) {
                        textView2.setVisibility(View.INVISIBLE);
                    }
                }
                ViewFlipper.LayoutParams params = new ViewFlipper.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.CENTER_VERTICAL;
                view.setLayoutParams(params);
                i++;
                viewflipper.addView(view);
                continue;
            }
            ViewFlipper.LayoutParams params = new ViewFlipper.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER_VERTICAL;
            view.setLayoutParams(params);
            TextView childAt = (TextView) view.getChildAt(0);
            childAt.setText(data.get(i));
//            childAt.setTextColor(Color.rgb(255,255,255));
            viewflipper.addView(view);
        }
        //滚动时间
        viewflipper.setFlipInterval(2000);
        //出入动画
        viewflipper.setOutAnimation(activity, R.anim.push_up_out);
        viewflipper.setInAnimation(activity, R.anim.push_down_in);
    }

    public void startFlipper(){
        if(viewflipper!=null) {
            viewflipper.setVisibility(View.VISIBLE);
            //是否自动开始滚动
            viewflipper.setAutoStart(true);
            //开始滚动
            viewflipper.startFlipping();
        }
    }
    public void startFlipperPos(int pos){
        if(list.size()!=0&&pos<list.size()) {
            ViewFlipper view = list.get(pos);
            if(view!=null) {
                view.setVisibility(View.VISIBLE);
                //是否自动开始滚动
                view.setAutoStart(true);
                //开始滚动
                view.startFlipping();
            }
        }
    }

    public void stopFlipperPos(int pos){
        if(pos<0) {
            return;
        }
        if(list.size()!=0&&pos<list.size()) {
            ViewFlipper view = list.get(pos);
            if(view!=null) {
                //停止滚动
                view.setVisibility(View.GONE);
                view.stopFlipping();
            }
        }
    }

    public void stopFlipper(){
        if(viewflipper!=null) {
            //停止滚动
            viewflipper.setVisibility(View.GONE);
            viewflipper.stopFlipping();
        }
    }


    public void startFlipperWithFname(String fname){
        if(viewflipper!=null) {
            String str = (String) viewflipper.getTag(R.id.tag_third);
            if(fname.equals(str)) {
                viewflipper.setVisibility(View.VISIBLE);
                //是否自动开始滚动
                viewflipper.setAutoStart(true);
                //开始滚动
                viewflipper.startFlipping();
            }
        }
    }

    public void stopFlipperWithFname(String fname){
        if(viewflipper!=null) {
            //停止滚动
            String str = (String) viewflipper.getTag(R.id.tag_third);
            if(fname.equals(str)) {
                viewflipper.setVisibility(View.GONE);
                viewflipper.stopFlipping();
            }
        }
    }
}
